package org.cscarioni

import net.sf.antcontrib.net.httpclient.Params;

class ChartController {
	def chartValuesCalculatorService
    def createChart = { 	
    	def entityWithEstimatedCollection = params.entity
		def sprint = Sprint.get(params.sprintId)
		def estimatedCollectionProperty = "estimatedTimes"
		def entity = getEntityFromParams(params)
		def chartMap = chartValuesCalculatorService.calculateChartMap(entity."$estimatedCollectionProperty",(sprint.startDate)..(sprint.endDate),entity.storyPoints)
		def expectedLineList = chartValuesCalculatorService.calculateToZeroProgressionForDays(entity.storyPoints,sprint.daysOfWork)
		render(view:"burnDownChart",model:['sprint':sprint,'chartData':chartMap,'expectedLineListData':expectedLineList,'startDate':sprint.startDate,'endDate':sprint.endDate,'maxValue':entity.storyPoints])
	}
	def createChartOnlyChartReturn = { 	
		def entityWithEstimatedCollection = params.entity
		def sprint = Sprint.get(params.sprintId)
		def estimatedCollectionProperty = "estimatedTimes"
		def entity = getEntityFromParams(params)
		def chartMap = chartValuesCalculatorService.calculateChartMap(entity."$estimatedCollectionProperty",(sprint.startDate)..(sprint.endDate),entity.storyPoints)
		def expectedLineList = chartValuesCalculatorService.calculateToZeroProgressionForDays(entity.storyPoints,sprint.daysOfWork)
		render(view:"burnDownChartOnlyChart",model:['sprint':sprint,'chartData':chartMap,'expectedLineListData':expectedLineList,'startDate':sprint.startDate,'endDate':sprint.endDate,'maxValue':entity.storyPoints])
	}
	def pruebaChart = { 	
		render(view:"burnDownChart")
	}
	def getEntityFromParams(params){
		if(params.userId && params.userId!="-1"){
			return User.get(params.userId)
		}
		else if(params.userId && params.teamId!="-1"){
			return Team.get(params.teamId)
		}
		else{
			return Sprint.get(params.sprintId)
		}
	}	
}
